package cn.hairui.blog.controller;

import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.entity.SRescManage;
import cn.hairui.blog.service.SRescManageService;
import cn.hairui.blog.service.SRescMenuService;
import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author laoganbu
 * @since 2020-04-08
 */
@Controller

public class SRescManageController {
    @Autowired
    private SRescManageService sRescManageService;
    @Autowired
    private SRescMenuService sRescMenuService;

    // 列表页面
    @RequestMapping(value = "/manage/sRescManage/list")
    public String list() {
        return "page/sRescManage/list";
    }

    // 新增页面
    @RequestMapping(value = "/manage/sRescManage/add")
    public String add() {
        return "page/sRescManage/add";
    }

    // 列表数据
    @RequestMapping(value = "/manage/sRescManage/data")
    @ResponseBody
    public String data(HttpServletRequest request) {
        Map map = new HashMap<Object, Object>();
        int pageSize = 15;// 默认每页显示6条记录
        int pageNum = 1;
        try {
            pageNum = Integer.parseInt(request.getParameter("page"));
            pageSize = Integer.parseInt(request.getParameter("limit"));
        } catch (Exception e) {
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Map> mapList = sRescManageService.list();
        PageInfo<Map> pageInfo = new PageInfo<Map>(mapList);
        map.put("data", mapList);
        map.put("code", 0);
        map.put("count", pageInfo.getTotal());
        return JSONUtils.toJSONString(map);
    }

    // 新增数据
    @RequestMapping(value = "/manage/sRescManage/addData")
    @ResponseBody
    @Transactional
    public String add(HttpServletRequest request, SRescManage sRescManage) {
        Map map = new HashMap<Object, Object>();
        // 通过名称判断资源id是否已经存在
        QueryWrapper<SRescManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("resc_id", sRescManage.getRescId());

        SRescManage result = sRescManageService.getOne(queryWrapper);
        if (result != null) {
            map.put(PubConstant.flag, PubConstant.exsit);
            map.put("type", "resc_id");
            return JSONUtils.toJSONString(map);
        }

        // 通过名称判断资源名称是否已经存在
        QueryWrapper<SRescManage> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("resc_name", sRescManage.getRescName());

        SRescManage result2 = sRescManageService.getOne(queryWrapper2);
        if (result2 != null) {
            map.put(PubConstant.flag, PubConstant.exsit);
            map.put("type", "resc_name");
            return JSONUtils.toJSONString(map);
        }

        if (sRescManage.getUseFlag() != null && PubConstant.ON.equals(sRescManage.getUseFlag())) {
            sRescManage.setUseFlag(PubConstant.YES_NO_Y);
        } else {
            sRescManage.setUseFlag(PubConstant.YES_NO_N);
        }
        sRescManageService.save(sRescManage);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    // 删除数据
    @RequestMapping(value = "/manage/sRescManage/delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public String delete(@Param(value = "rescId") String rescId, HttpServletRequest request) {
        Map map = new HashMap();
        QueryWrapper<SRescManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("resc_id", rescId);
        // 通过rescId查询 S_RESC_MENU表中是否已经配置菜单
        int mcount = sRescMenuService.countByRescId(rescId);
        if (mcount > 0) {
            map.put(PubConstant.flag, PubConstant.exsit);
            return JSONUtils.toJSONString(map);
        }
        sRescManageService.remove(queryWrapper);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    // 修改页面
    @RequestMapping(value = "/manage/sRescManage/update")
    public String categoryUpdate(HttpServletRequest request, Model model) {
        String rescId = String.valueOf(request.getParameter("rescId"));
        SRescManage sRescManage = sRescManageService.getByRescId(rescId);
        model.addAttribute("sRescManage", sRescManage);
        return "page/sRescManage/update";
    }

    // 更新数据
    @RequestMapping(value = "/manage/sRescManage/updateData")
    @ResponseBody
    @Transactional
    public String updateData(HttpServletRequest request, SRescManage sRescManage) {
        Map map = new HashMap<Object, Object>();
        if (sRescManage.getUseFlag() != null && PubConstant.ON.equals(sRescManage.getUseFlag())) {
            sRescManage.setUseFlag(PubConstant.YES_NO_Y);
        } else {
            sRescManage.setUseFlag(PubConstant.YES_NO_N);
        }
        sRescManageService.saveOrUpdate(sRescManage);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }
}
